﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.CoreBusinessLogic.Web;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.CoreBusinessLogic.PaymentVoucher;
using SCMS.CoreBusinessLogic.Security;
using SCMS.Reports;

namespace SCMS.UI.Areas.Reports.Controllers
{
    [MyException]
    public class PVoucherController : BaseController
    {
        private IPaymentVoucherService pVService;

        public PVoucherController(IPaymentVoucherService pVServiceService)
        {
            this.pVService = pVServiceService;
        }

        //
        // GET: /Reports/PVoucher/

        public ActionResult Index(Guid PVId)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            Model.PaymentVoucher grnentity = pVService.GetPaymentVoucherById(PVId);

            data.Add("{REQ_NO}", grnentity.RefNumber);
            data.Add("{PNAME}", grnentity.PaymentRequest.PurchaseOrder.ProjectDonor.Project.ShortName);
            data.Add("{PNUMBER}", grnentity.PaymentRequest.PurchaseOrder.ProjectDonor.ProjectNumber);

            data.Add("{DATE}", grnentity.PreparedOn.Value.ToString("dd/MM/yyyy"));
            data.Add("{RFPNO}", grnentity.PaymentRequest.RefNumber);
            data.Add("{PONO}", grnentity.PaymentRequest.PurchaseOrder.RefNumber);

            data.Add("{PAYMENTMODE}", grnentity.PaymentRequest.PaymentType.Description);
            data.Add("{PBNKACC}", grnentity.ProjectBankAccount);

            int r = 1;
            String detailHtml = "";

            foreach (var grnitem in grnentity.PaymentVoucherBudgetLines)
            {
                detailHtml += "<tr><td style=\"text-align:left;\">";
                detailHtml += grnitem.PaymentRequestBudgetLine.ProjectBudget.LineNumber;
                detailHtml += "</td><td style=\"text-align:left;\">";
                detailHtml += grnitem.Description;
                detailHtml += "</td><td style=\"text-align:left;\">";
                detailHtml += grnitem.GeneralLedger.Code;
                detailHtml += "</td><td style=\"text-align: right;\">";
                detailHtml += grnitem.Amount.Value.ToString("##,##0.00");
                detailHtml += "</td></tr>"; r++;
            }
            while (r < 18)
            {
                detailHtml += "<tr><td style=\"text-align:left;\">&nbsp;";
                detailHtml += "</td><td style=\"text-align:left;\">";
                detailHtml += "</td><td style=\"text-align:left;\">";
                detailHtml += "</td><td style=\"text-align: right;\">";
                detailHtml += "</td></tr>"; r++;
            }
            data.Add("{DETAILS}", detailHtml);
            data.Add("{TAMOUNT}", grnentity.TotalAmount.Value.ToString("##,##0.00"));
            data.Add("{AMOUNT_IN_WORDS}", grnentity.AmountInWords);
            data.Add("{SUPPLIER}", grnentity.PaymentRequest.Supplier.Name);

            String authourisation = "";
            authourisation = AuthName(grnentity, authourisation);
            authourisation = AuthTitle(grnentity, authourisation);
            authourisation = AuthDate(grnentity, authourisation);
            authourisation = AuthSignature(grnentity, authourisation);
            data.Add("{AUTHORISATION}", authourisation);

            List<String> options = new List<string>();
            options.Add(" --copies 2 ");
            Byte[] output = WkHtml2Pdf.CreateReport(data, "Payment_Voucher.htm", options);

            return File(output, "application/pdf", "PV_" + DateTime.Now.FormatDDMMMYYYYHHmm());
        }


        private static String AuthName(Model.PaymentVoucher Model, String authourisation)
        {
            authourisation += "<tr><th>Name:</th><td>";
            authourisation += Model.PreparedByStaff.StaffName;
            authourisation += "</td><td>";
            if (Model.AuthourisedByStaff != null)
                authourisation += Model.AuthourisedByStaff.StaffName;
            authourisation += "</td><td></td><tr>";
            return authourisation;
        }

        private static String AuthTitle(Model.PaymentVoucher Model, String authourisation)
        {
            authourisation += "<tr><th>Title:</th><td>";
            authourisation += Model.PreparedByStaff.Designation;
            authourisation += "</td><td>";
            if (Model.AuthourisedByStaff != null)
                authourisation += Model.AuthourisedByStaff.Designation;
            authourisation += "</td><td></td><tr>";
            return authourisation;
        }

        private static String AuthDate(Model.PaymentVoucher Model, String authourisation)
        {
            authourisation += "<tr><th>Date:</th><td>";
            authourisation += Model.PreparedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><td>";
            if (Model.AuthorizedOn != null)
                authourisation += Model.AuthorizedOn.Value.ToString("dd/MM/yyyy h:mm tt");
            authourisation += "</td><td></td><tr>";
            return authourisation;
        }

        private static String AuthSignature(Model.PaymentVoucher Model, String authourisation)
        {
            authourisation += "<tr><th>Signature:</th><td>";
            if (Model.PreparedByStaff.SignatureImage != null)
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.PreparedByStaff.SignatureImage));
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null && Model.AuthourisedByStaff.SignatureImage != null)
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.AuthourisedByStaff.SignatureImage));
            authourisation += "</td><td></td><tr>";
            return authourisation;
        }

    }
}
